package br.com.async.core.application.impl;

import java.util.Calendar;

import org.hibernate.Criteria;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import br.com.async.core.application.BookmarkApplication;
import br.com.async.core.domain.bookmark.Bookmark;
import br.com.async.core.domain.bookmark.BookmarkRepository;
import br.com.async.core.domain.customer.Customer;
import br.com.async.core.domain.customer.CustomerRepository;
import br.com.async.core.domain.enterprise.Enterprise;
import br.com.async.core.domain.enterprise.EnterpriseRepository;
import br.com.async.core.infrastructure.AbstractRepositoryImpl;

@Transactional(propagation = Propagation.REQUIRED)
public class BookmarkApplicationImpl implements BookmarkApplication {

	private CustomerRepository customerApplication;
	private EnterpriseRepository enterpriseApplication;
	private BookmarkRepository bookmarkRepository;

	public BookmarkApplicationImpl(CustomerRepository customerApplication,
			EnterpriseRepository enterpriseApplication, BookmarkRepository bookmarkRepository) {
		super();
		this.customerApplication = customerApplication;
		this.enterpriseApplication = enterpriseApplication;
		this.bookmarkRepository = bookmarkRepository;
	}
	
	

	@Override
	public void markAsBookmark(Integer customerId, Integer enterpriseCode) {

		Customer customer = customerApplication.findById(customerId);
		Enterprise enterprise = enterpriseApplication.findById(enterpriseCode);

		Bookmark bookmark = new Bookmark(Calendar.getInstance(), enterprise);

		customer.getBookmark().add(bookmark);

		try {
			customerApplication.update(customer);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	public Boolean isBookmarked(Integer customerCode, Integer EnterpriseCode){
		return bookmarkRepository.isBookmarked(customerCode, EnterpriseCode);
	}

}
